// 证券冻结解冻表格页面
import React from 'react';
import { connect } from 'dva';
import { stringify } from 'qs';
import { PrimaryC, TableC, PrimaryGhostC } from '../../../../comps';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { exportFile } from '@/src/utils/commonFunc';
import { findValueByCode } from '@/src/utils/utils';

@Form.create()
class securityUnfreeze extends React.Component {
  state = {
    unfreezeVisible: false, // 模态框
    buttonOk: true,
    tableData: {},
    value: '',
    rowData: '',
    userName: '',
  };
  componentDidMount() {
    this.setState({
      buttonOk: true,
    });
    this.props.onRef(this);
    this.props.dispatch({
      type: 'positionManageM/getUser',
      payload: localStorage.getItem('antd-pro-superManId'),
      callback: (data) => {
        if (data) {
          this.setState({
            userName: data.data.name,
          });
        }
      },
    });
  }
  //表格页码排序改变调用
  handleTableChange = (pagination) => {
    this.props.handleTableChange(pagination);
    this.setState({
      buttonOk: true,
      rowData: '',
    });
  };

  // 表格数据点击响应
  tableChange = (record) => {
    if (record) {
      if (record.operateType == 4 || record.operateType == 5) {
        let id = record.combiId;
        const value = {
          amount: record.amount.toString(),
          dataDt: record.dataDt,
          miniUnitId: record.combiCode,
          operateBy: this.state.userName,
          operateType: record.operateType == 4 ? '4_1' : '5_1',
          securityType: record.securityType,
          tradingcode: record.tradingcode,
          exchangeCode: record.exchangeCode,
        };
        this.props.dispatch({
          type: 'positionManageM/getJurisdiction',
          payload: {
            id,
          },
          callback: (data) => {
            if (data && data.dataSource.operationPermission) {
              this.setState({
                buttonOk: false,
                tableData: value,
              });
            }
          },
        });
      } else {
        this.setState({
          buttonOk: true,
          tableData: {},
        });
      }
    }
  };

  // 按钮点击响应
  buttonClick = () => {
    this.props.dispatch({
      type: 'positionManageM/adjustSecurity',
      payload: this.state.tableData,
      callback: (res) => {
        if (res) {
        }
      },
    });
  };
  // 导出
  export = () => {
    const { queryItems } = this.props;
    const values = {
      dataDt: queryItems.dataDt,
      fundId: queryItems.fundId?.join(),
      exchangecodes: queryItems.exchangeCode,
      tradingcodes: queryItems.tradingCode,
      investmentType: queryItems.investmentType,
      userId: localStorage.getItem('antd-pro-userId'),
    };
    exportFile('api/v1/holdInfo/exportfrost', values);
  };
  onRef = () => {
    this.setState({
      buttonOk: true,
      rowData: '',
    });
  };
  render() {
    const {
      globalM: {
        dataDictionary: { ENTRUSTED_INVESTMENT_HOLD_ATTRCODE = [] },
      },
    } = this.props;
    let { columns, dataSource, total, currentPage, pageSize } = this.props;
    const { buttonOk, unfreezeVisible, key, tableData, rowData } = this.state;
    columns.map((item) => {
      if (item.key == 'shortBlankSign') {
        item.render = (text) => {
          return findValueByCode(text, ENTRUSTED_INVESTMENT_HOLD_ATTRCODE);
        };
      }
    });
    return (
      <div>
        <div style={{ marginTop: '10px' }}>
          <PrimaryC title={'取消冻结解冻'} style={{ margin: '5px' }} disabled={buttonOk} onClick={() => this.buttonClick()} />
          {/* <PrimaryGhostC
                        title="导出"
                        style={{ margin: '10px', float: 'right' }}
                        onClick={this.export}
                    /> */}
        </div>
        <div style={{ marginTop: '10px' }}>
          <TableC
            columns={columns}
            loading={this.props.loading}
            dataSource={dataSource}
            current={currentPage}
            pageSize={pageSize}
            total={total}
            scroll={{ x: 'max-content' }}
            rowKey='id'
            pagination={true}
            onChange={this.handleTableChange}
            onRow={(record, index) => ({
              onClick: () => {
                this.tableChange(record);
                this.setState({
                  rowData: index,
                });
              },
            })}
            rowClassName={(record, index) => {
              return index === rowData ? 'trade-select' : '';
            }}
          />
        </div>
      </div>
    );
  }
}

export default connect(({ positionManage, globalM, loading, positionInquiryM }) => ({
  positionManage,
  positionInquiryM,
  globalM,
  loading: loading.effects['positionManageM/getUnfreezeList'],
}))(securityUnfreeze);
